java如何在安卓 studio中使列表中的项目可单击并引导到具有特定格式的新页面
我试图在安卓中创建一个具有列表视图的页面,然后当你点击列表中的一个项目时,它应该会导致一个具有特定格式的新活动
基本上,它是一个成绩中心页面,包含你的课程列表,当你点击一门课程时,它会打开一个课程页面
我创建了一个成绩中心活动,一个单一的课程页面活动和他们各自的班级
现在这两个页面已经存在,但它们没有相互链接,这实际上是一个包含许多其他页面的巨大文件,等等,但我只是发布了我需要帮助的部分,但如果有必要,我也可以发布其他部分。我希望列表中的元素是可点击的,然后引导到用户点击的课程页面,比如其中一门课程是Biol 116,那么列表应该带你到Biol 116课程页面。提前感谢您的帮助!:)
成绩中心xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/activity_grades_centre"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.gregmallan.yuboard.GradesCentre"><![CDATA[
安卓:clickable="true">
]]>
<TextView
安卓:text="Current Overall Grade:"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:id="@+id/textView3"
安卓:textColor="@color/colorPrimaryDark"
安卓:textSize="20sp"
安卓:layout_alignParentBottom="true"
安卓:layout_alignParentStart="true"
安卓:layout_marginBottom="21dp" />
<ListView
安卓:id="@+id/CourseGradeList"
安卓:layout_height="350dp"
安卓:layout_width="230dp"
安卓:layout_alignParentTop="true"
安卓:layout_alignParentStart="true"
安卓:layout_marginTop="25dp" />
<TextView
安卓:text="85.67"
安卓:layout_height="40dp"
安卓:id="@+id/Overall"
安卓:layout_width="80dp"
安卓:textSize="25sp"
安卓:textColor="@安卓:color/holo_red_light"
安卓:layout_alignTop="@+id/textView3"
安卓:layout_toEndOf="@+id/CourseGradeList"
安卓:layout_marginStart="17dp" />
成绩中心java文件:
public class GradesCentre extends AppCompatActivity {
double OverallAvg;
ArrayList<Course> AllCourses;
ArrayAdapter<Course> AdapterCourse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grades_centre);
AllCourses = new ArrayList<>();
AllCourses.add(new Course("Cosc 111"));
AllCourses.add(new Course("Biol 116"));
AllCourses.add(new Course("Anth 100"));
AllCourses.get(0).setCourseGrade(95);
AllCourses.get(1).setCourseGrade(78);
AllCourses.get(2).setCourseGrade(84);
OverallAvg = calcOverallGrade(AllCourses);
AdapterCourse = new ArrayAdapter<Course>(this , 安卓.R.layout.simple_list_item_1 , AllCourses);
ListView listView = (ListView) findViewById(R.id.CourseGradeList);
listView.setAdapter(AdapterCourse);
}
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
}
public double calcOverallGrade(ArrayList<Course> Anything){
double sum = 0;
for(int i = 0; i < Anything.size();i++ ){
sum = Anything.get(i).getCourseGrade() + sum;
}
OverallAvg = sum/(Anything.size());
return OverallAvg;
}
}
单课程页面xml:
<TextView
安卓:text="_________________________________________________"
安卓:layout_width="350dp"
安卓:layout_height="wrap_content"
安卓:id="@+id/textView10"
安卓:textStyle="normal|bold"
安卓:textColor="@color/colorPrimaryDark"
安卓:layout_below="@+id/SingleCourseGrade"
安卓:layout_alignParentStart="true" />
<TextView
安卓:text="_________________________________________________"
安卓:layout_width="350dp"
安卓:layout_height="wrap_content"
安卓:id="@+id/textView9"
安卓:textStyle="normal|bold"
安卓:textColor="@color/colorPrimaryDark"
安卓:layout_below="@+id/SingleCourseGrade"
安卓:layout_alignParentStart="true" />
<TextView
安卓:text="ex. Course 1"
安卓:layout_alignParentTop="true"
安卓:layout_alignParentStart="true"
安卓:id="@+id/CourseName"
安卓:layout_height="30dp"
安卓:layout_width="200dp"
安卓:textSize="24sp"
安卓:textColor="@color/colorPrimaryDark"
安卓:textStyle="normal|bold" />
<TextView
安卓:text="Current Overall Grade: "
安卓:layout_height="30dp"
安卓:layout_below="@+id/CourseName"
安卓:layout_alignParentStart="true"
安卓:layout_marginTop="15dp"
安卓:id="@+id/textView5"
安卓:textSize="20sp"
安卓:textColor="@color/colorPrimaryDark"
安卓:layout_width="200dp" />
<Button
安卓:text="Add/Manage Components"
安卓:layout_height="50dp"
安卓:id="@+id/AddManage"
安卓:layout_width="350dp"
安卓:layout_alignParentBottom="true"
安卓:layout_alignParentEnd="true" />
<ListView
安卓:id="@+id/SingleCourseGradeList"
安卓:layout_above="@+id/AddManage"
安卓:layout_alignStart="@+id/AddManage"
安卓:layout_height="315dp"
安卓:layout_width="280dp" />
<TextView
安卓:text="%%%"
安卓:layout_marginEnd="14dp"
安卓:id="@+id/SingleCourseGrade"
安卓:textSize="20sp"
安卓:textColor="@安卓:color/holo_red_light"
安卓:layout_alignParentEnd="true"
安卓:layout_width="60dp"
安卓:layout_height="25dp"
安卓:layout_alignTop="@+id/textView5" />
<TextView
安卓:text="_________________________________________________"
安卓:layout_width="350dp"
安卓:layout_height="wrap_content"
安卓:id="@+id/textView8"
安卓:textStyle="normal|bold"
安卓:textColor="@color/colorPrimaryDark"
安卓:layout_above="@+id/textView5"
安卓:layout_alignParentStart="true" />
<TextView
安卓:text="Individual Components and Grades:"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:id="@+id/textView7"
安卓:textSize="16sp"
安卓:textColor="@color/colorPrimaryDark"
安卓:layout_above="@+id/SingleCourseGradeList"
安卓:layout_alignStart="@+id/SingleCourseGradeList" />
</RelativeLayout>
单课程页面java文件:
import 安卓.content.Intent;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.Button;
import 安卓.widget.ListView;
import 安卓.widget.TextView;
import com.gregmallan.yuboard.SchoolRelatedClasses.CourseComponent;
import java.util.ArrayList;
public class SingleCoursePage extends AppCompatActivity{
ArrayAdapter<CourseComponent> adapter;
Button Components;
Button CompDetail;
double CourseGrade;
ArrayList<CourseComponent> AllComponents;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_course_page);
//this is just added until we get real data
AllComponents = new ArrayList<CourseComponent>();
CourseComponent x = new CourseComponent("Dummy", 20);
AllComponents.add(x);
CourseComponent x2 = new CourseComponent("Dummy2", 40);
AllComponents.add(x2);
adapter = new ArrayAdapter<CourseComponent>(this , 安卓.R.layout.simple_list_item_1 , AllComponents);
ListView listView = (ListView) findViewById(R.id.SingleCourseGradeList);
listView.setAdapter(adapter);
Components = (Button) findViewById(R.id.AddManage);
Components.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
//Inform the user the button has been clicked
openCourseComponents(v);
}
});
}
public void openCourseComponents(View view){
Intent intent1 = new Intent(this, IndividualCourseCentre.class);
startActivity(intent1);
}
//public void openComponentBreakdown(View view){
// Intent intent2 = new Intent(this, IndividualCourseCentre.class);
// startActivity(intent2);
//}
public double calcCourseGrade(ArrayList<CourseComponent> x){
double sum = 0;
for(int i = 0; i < x.size();i++){
sum = x.get(i).getTotalComponentGrade();
}
CourseGrade = sum/(x.size());
return CourseGrade;
//TO-DO
//when this method is called changed the text to the CourseGrade
}
}
# 1 楼答案
您可以使用以下代码:
在SingleCourseActivity中获取如下对象:
希望这有帮助